import { getLocalStorage, toLogOnPage } from '../../utils.js'
const App = getApp()

Page({
  data: {
    openid: null,
    items: [{
      icon: '../../images/iconfont-order.png',
      text: '我的余额',
      path: '/pages/order/list/index',
      value: 0
    }, {
      icon: '../../images/iconfont-order.png',
      text: '我的订单',
      path: '/pages/order/list/index'
    }, {
      icon: '../../images/iconfont-addr.png',
      text: '完善个人资料',
      path: '/pages/address/index'
    }, {
      icon: '../../images/iconfont-kefu.png',
      text: '联系客服',
      value: '18521708248'
    }]
  },
  onReady() {
    const tabBar = this.getTabBar()
    tabBar && tabBar.setData({ active: "setting" })
  },
  async onShow() {
    wx.showLoading({ title: '加载中', mask: true })
    const openid = await getLocalStorage('openid')
    if (!openid) {
      return wx.hideLoading()
    }
    this.setData({ openid })
    const { result } = await wx.cloud.callFunction({ name: 'rh', data: { type: 'getTotalMoney', openid } })
    if (!result) {
      return wx.hideLoading()
    }
    const items = this.data.items.map(i => {
      i.value = i.text === '我的余额' ? (result.money + ' 元') : i.value
      return i
    })
    this.setData({ items })
    wx.hideLoading()
  },
  handleClick(e) {
    const i = e.currentTarget.dataset.item
    if(i.text === '联系客服') {
      return
    }

    if(!this.data.openid) {
      wx.showModal({ title: '账号未登录', confirmText: '前往登录',
        success (r) {
          r.confirm && toLogOnPage()
        }
      })
      return
    }
    if(i.text === '完善个人资料') {
      wx.navigateTo({ url: '/pages/address/index' })
    }
    if (i.text === '我的余额') {
      this.toAddMoneyPage()
    } else {
      wx.navigateTo({ url: '/pages/orderList/index' })
    }
  },
  navigateTo(e) {
    const index = e.currentTarget.dataset.index
    const path = e.currentTarget.dataset.path

    switch (index) {
      case 2:
        App.WxService.makePhoneCall({
          phoneNumber: path
        })
        break
      default:
        App.WxService.navigateTo(path)
    }
  },
  // getUserInfo() {
  // 	const userInfo = App.globalData.userInfo

  // if (userInfo) {
  // 	this.setData({
  // 		userInfo: userInfo
  // 	})
  // 	return
  // }

  // App.getUserInfo()
  // .then(data => {
  //         console.log(data)
  // 	this.setData({
  // 		userInfo: data
  // 	})
  // })
  // },
  // getStorageInfo() {
  // 	App.WxService.getStorageInfo()
  // 	.then(data => {
  // 		console.log(data)
  // 		this.setData({
  // 			'settings[0].path': `${data.currentSize}KB`
  // 		})
  // 	})
  // },
  bindtap(e) {
    const index = e.currentTarget.dataset.index
    const path = e.currentTarget.dataset.path

    switch (index) {
      case 0:
        App.WxService.showModal({
            title: '友情提示',
            content: '确定要清除缓存吗？',
          })
          .then(data => data.confirm == 1 && App.WxService.clearStorage())
        break
      default:
        App.WxService.navigateTo(path)
    }
  },
  logout() {
    App.WxService.showModal({
        title: '友情提示',
        content: '确定要登出吗？',
      })
      .then(data => data.confirm == 1 && this.signOut())
  },
  signOut() {
    App.HttpService.signOut()
      .then(res => {
        const data = res.data
        console.log(data)
        if (data.meta.code == 0) {
          App.WxService.removeStorageSync('token')
          App.WxService.redirectTo('/pages/login/index')
        }
      })
  },
  toLogOnPage,
  toAddMoneyPage() {
    const pageThis = this
    wx.navigateTo({
      url: '/pages/addMoney/index',
      events: {
        acceptDataFromOpenedPage: function(data) {
        }
      }
    })
  }
})